# -*- codeing = utf-8 -*-
# @Time : 2021/2/28 19:14
# @Atuthor: 朱朱
# @File: index.py
# @Software: PyCharm
from 学生管理系统015 import file_manager
from 学生管理系统015 import model
from 学生管理系统015 import student_manager
from 学生管理系统015 import tools

USERS_FILE = 'teacher.json'


def login():
    all_ther = file_manager.read_json(USERS_FILE, {})
    ther_name = input('请输入老师账号:')
    if ther_name not in all_ther:
        print('登录失败,该账号没有注册')
        return
    else:
        password = input('请输入密码:')
        if tools.encrypt_password(password) == all_ther[ther_name]:
            print('登录成功')
            student_manager.USER_NAME = ther_name
            student_manager.show_system()
        else:
            print('密码有误,退出')


def register():
    all_ther = file_manager.read_json(USERS_FILE,{})
    while True:
        ther_name = input('请输入老师账号(3-6位):')
        if not 3 <= len(ther_name) <= 6:
            print('错误:请输入3-6的账号信息')
        else:
            break
    if ther_name in all_ther:
        print('注册失败,该账号已经注册过')
        return
    while True:
        password = input('请输入密码6-12位:')
        if not 6 <= len(password) <= 12:
            print('密码不符合要求,输入密码6-12位')
        else:
            break
    t = model.Teacher(ther_name,password) # 创建teacher对象(名字,密码)
    all_ther[t.name] = t.password   # 形成键值对key value
    file_manager.write_json(USERS_FILE,all_ther)
    print('注册成功')

def start():
    content = file_manager.read_text('welcome.txt')
    while True:
        print(content)
        value = input('请选择(1-3):')
        if value == '1':
            login()
        elif value == '2':
            register()
        elif value == '3':
            print('退出成功')
            exit()
        else:
            print('输入有误')

if __name__ == '__main__':
    start()